﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LP580.CG;

public partial class Manage_CG_GiftProductClassList : System.Web.UI.Page
{
    private string act = Utils.RQ_str("act").ToLower();
    protected int id = Utils.RQ_int("id");
    private string searchKey = Utils.RQ_str("sk");
    private int pageIndex = Utils.RQ_int("pi");
    private int pageSize = Utils.RQ_int("pz");
    protected void Page_Load(object sender, EventArgs e)
    {
        clsAdmin adm = clsAdmin.getCookieAdmin();
        if (adm == null)
        {
            Utils.WrAlertGo("您尚未成功登陆，请成功登陆后再进行正常操作！", "../Login.aspx");
        }
        if (pageSize == 0) pageSize = 10;
        switch (act)
        {
            case "up":
                DealUpOrDown(true);
                break;
            case "down":
                DealUpOrDown(false);
                break;
            case "del":
                Delete();
                break;
        }
        if (!IsPostBack)
            bindData();
        bindContrl();
    }

    private void DealUpOrDown(bool up)
    {
        int cid = Utils.RQ_int("cid");
        clsGiftProductClass cls = new clsGiftProductClass(cid);
        if (up)
        {
            clsGiftProductClass.MoveGoUp(cls);
        }
        else
        {
            clsGiftProductClass.MoveGoDown(cls);
        }
        string url = string.Format("GiftProductClassList.aspx?pi={0}&pz={1}&id={2}", pageIndex, pageSize, id);
        Response.Redirect(url, true);
    }

    private void Delete()
    {
        clsGiftProductClass cls = new clsGiftProductClass(id);
        if (cls.HasChild)
        {
            Utils.WrAlertGoBack("有子类不可删除！");
        }
        else
        {
            clsGiftProductClass.Delete(id);
        }
        Response.Redirect("GiftProductClassList.aspx", true);
    }

    private void bindContrl()
    {

    }

    private void bindData()
    {

        PagedDataSource pds = new PagedDataSource();
        if (id > 0)
        {
            clsGiftProductClass cls = new clsGiftProductClass(id);
            pds.DataSource = cls.GetDirectChildCollection();
            string strPath = "";
            List<clsGiftProductClass> ls = cls.GetParentCollection();
            foreach (clsGiftProductClass c in ls)
            {
                strPath += "-[<a href='GiftProductClassList.aspx?id=" + c.GiftProductClassID + "'>" + c.GiftProductClassName + "</a>]";
            }
            ltClassUrl.Text = strPath;
        }
        else
        {
            clsGiftProductClass cls = new clsGiftProductClass();
            pds.DataSource = cls.GetDirectChildCollection();
            //pds.DataSource = clsGiftProductClass.GetCollection();
        }
        pds.PageSize = pageSize;
        pds.CurrentPageIndex = (pageIndex - 1 < 0) ? 0 : (pageIndex - 1);
        pds.AllowPaging = true;

        string baseUrl = "GiftProductClassList.aspx?id=" + id;
        ltPage.Text = LvEasyPager.Create(baseUrl + "&pi={0}&pz={1}", pageIndex, pageSize, pds.DataSourceCount);

        gvDatalist.DataSource = pds;
        gvDatalist.PageIndex = pageIndex;
        gvDatalist.DataBind();
    }

    protected void lkbtnBatchDel_Click(object sender, EventArgs e)
    {
        List<clsGiftProductClass> ls = getDelList();
        bool isErr = false;
        string ErrMsg = "以下不可删除：\\n\\n";
        if (ls.Count > 0)
        {
            foreach (clsGiftProductClass s in ls)
            {
                bool CanDel = !s.HasChild;
                if (CanDel) //可删除
                {
                    clsGiftProductClass.Delete(s.GiftProductClassID);
                }
                else
                {
                    isErr = true;
                    ErrMsg += " 有子类别 " + s.GiftProductClassName;
                }
            }
        }
        string url = string.Format("GiftProductClassList.aspx?pi={0}&pz={1}&id={2}", pageIndex, pageSize, id);
        if (isErr)
        {
            Utils.WrAlertGo(ErrMsg, url);
        }
        else
        {
            Response.Redirect(url, true);
        }
    }

    private List<clsGiftProductClass> getDelList()
    {
        string ids = Utils.RF_str("ids");
        List<clsGiftProductClass> ls = new List<clsGiftProductClass>();
        if (ids != "")
        {
            string[] arrID = ids.Split(',');
            int arrLen = arrID.Length;
            for (int i = 0; i < arrLen; i++)
            {
                int id = 0;
                if (!int.TryParse(arrID[i].Trim(), out id))
                    id = 0;
                if (id > 0)
                {
                    clsGiftProductClass s = new clsGiftProductClass(id);
                    if (s.GiftProductClassID > 0)
                        ls.Add(s);
                }
            }
        }
        return ls;
    }

    protected string GetOP(int prmId)
    {
        string rv = "";

        if (new clsGiftProductClass(prmId).HasChild)
        {
            rv += string.Format("[<a href='GiftProductClassList.aspx?id={0}'>下级</a>]", prmId);

        }
        else
        {
            rv += "无子类";
        }
        rv += "&nbsp;";
        rv += string.Format("[<a href='GiftProductClassList.aspx?pi={0}&pz={1}&act=up&id={2}&cid={3}' title='上移'>↑</a> <a href='GiftProductClassList.aspx?pi={0}&pz={1}&act=down&id={2}&cid={3}' title='下移'>↓</a>]", pageIndex, pageSize, id, prmId);
        rv += "&nbsp;";
        rv += string.Format("[<a href='GiftProductClassOperate.aspx?act=add&pid={0}'>新增子类</a>]", prmId);
        rv += "&nbsp;";
        rv += string.Format("[<a href='GiftProductClassOperate.aspx?act=edit&id={0}'>编辑</a>]", prmId);
        rv += string.Format("[<a href='GiftProductClassList.aspx?act=del&id={0}' onclick='return confirm('删除此项，操作无法恢复。\\n\\n确实要执行吗？');return false;' >删除</a>]", prmId);

        return rv;
    }
}